package com.cloudpark.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.sf.cglib.beans.BeanMap;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cloudpark.common.util.Constants;
import com.cloudpark.common.util.ObjectUtil;
import com.cloudpark.common.util.Pagination;
import com.cloudpark.model.entity.Park;
import com.cloudpark.model.entity.ParkOrder;
import com.cloudpark.model.entity.ParkRecord;
import com.cloudpark.model.mapper.ParkOrderMapper;
import com.cloudpark.model.mapper.ParkRecordMapper;
import com.cloudpark.model.vo.ParkOrderVo;
import com.cloudpark.model.vo.ParkRecordVo;
import com.cloudpark.service.ParkRecordService;
import com.cloudpark.service.ParkService;

@Service
public class ParkRecordServiceImpl implements ParkRecordService {

	@Autowired
	private ParkRecordMapper parkRecordMapper;
	@Autowired
	private ParkOrderMapper parkOrderMapper;
	@Autowired
	private ParkService parkService;

	@Override
	public ParkRecord queryParkRecordByOrderNo(String orderNo) {
		List<ParkRecord> list = parkRecordMapper.selectByOrderNo(orderNo);
		if (ObjectUtil.isNotBlank(list)) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public List<ParkRecord> queryParkRecordList(ParkRecord parkRecord) {
		return parkRecordMapper.selectBySelective(parkRecord);
	}

	@Override
	public void save(ParkRecord parkRecord) {
		if (null != parkRecord.getId() && parkRecord.getId() > Constants.CONST_ZERO) {
			parkRecordMapper.updateByPrimaryKeySelective(parkRecord);
		} else {
			parkRecordMapper.insert(parkRecord);
		}
	}

	@Override
	public Pagination queryPardRecordList(Map<String, String> params,
			int start, int length, Long estateId) {
		
		Pagination pagination = new Pagination();
        String plateNumber = null == params.get("plateNumber") ? "" : params.get("plateNumber").toString();
        String parkCode = null == params.get("parkCode") ? "" : params.get("parkCode").toString();
        String carStatus = null == params.get("carStatus") ? "" : params.get("carStatus").toString();
        String releaseType = null == params.get("releaseType") ? "" : params.get("releaseType").toString();
        ParkRecord paramVo = new ParkRecord();
        if (StringUtils.isNotBlank(plateNumber)) {
            paramVo.setPlateNumber(plateNumber);
        }
        if (StringUtils.isNotBlank(carStatus)) {
            paramVo.setCarStatus(Integer.valueOf(carStatus));
        }
        if (StringUtils.isNotBlank(parkCode)) {
            paramVo.setParkCode(parkCode);
        }
        if (ObjectUtil.isNotBlank(carStatus)) {
        	paramVo.setCarStatus(Integer.valueOf(carStatus));
        }
        if (ObjectUtil.isNotBlank(releaseType)) {
        	paramVo.setReleaseType(Integer.valueOf(releaseType));
        }
        paramVo.setEstateId(estateId);
        int totalRows = parkRecordMapper.countBySelective(paramVo);
        paramVo.setCurrentPage(start / length + 1);
        paramVo.setNumPerPage(length);
        paramVo.setTotalRows(totalRows);
        paramVo.initPageParams();
        pagination.setTotalRows(totalRows);
        List<ParkRecord> list = parkRecordMapper.selectBySelective(paramVo);
        List<ParkRecordVo> retList = new ArrayList<ParkRecordVo>();
        if (ObjectUtil.isNotBlank(list)) {
        	for (ParkRecord parkRecord : list) {
        		ParkRecordVo parkRecordVo = new ParkRecordVo();
        		BeanMap source = BeanMap.create(parkRecord);
        		BeanMap.create(parkRecordVo).putAll(source);
        		Park park = parkService.queryByParkCode(parkRecord.getParkCode());
        		parkRecordVo.setParkName(park.getName());
        		retList.add(parkRecordVo);
        	}
        }
        pagination.setResultList(retList);
        return pagination;
	}

	@Override
	public Pagination queryOrderRecordList(Map<String, String> params,
			int start, int length, Long estateId) {
		Pagination pagination = new Pagination();
        String plateNumber = null == params.get("plateNumber") ? "" : params.get("plateNumber").toString();
        String parkCode = null == params.get("parkCode") ? "" : params.get("parkCode").toString();
        String payChannel = null == params.get("payChannel") ? "" : params.get("payChannel").toString();
        String carType = null == params.get("carType") ? "" : params.get("carType").toString();
        String carStatus = null == params.get("carStatus") ? "" : params.get("carStatus").toString();
        ParkOrderVo paramVo = new ParkOrderVo();
        if (StringUtils.isNotBlank(plateNumber)) {
            paramVo.setPlateNumber(plateNumber);
        }
        if (StringUtils.isNotBlank(parkCode)) {
            paramVo.setParkCode(parkCode);
        }
        if (ObjectUtil.isNotBlank(payChannel)) {
        	paramVo.setPayChannel(Integer.valueOf(payChannel));
        }
        if (ObjectUtil.isNotBlank(carType)) {
        	paramVo.setCarType(Integer.valueOf(carType));
        }
        if (ObjectUtil.isNotBlank(carStatus)) {
            paramVo.setCarStatus(Integer.valueOf(carStatus));
        }
        paramVo.setEstateId(estateId);
        int totalRows = parkOrderMapper.countByCondition(paramVo);
        paramVo.setCurrentPage(start / length + 1);
        paramVo.setNumPerPage(length);
        paramVo.setTotalRows(totalRows);
        paramVo.initPageParams();
        pagination.setTotalRows(totalRows);
        List<ParkOrderVo> list = parkOrderMapper.selectByCondition(paramVo);
        if (ObjectUtil.isNotBlank(list)) {
        	
        } else {
        	list = new ArrayList<ParkOrderVo>();
        }
        pagination.setResultList(list);
        return pagination;
	}
}
